//当日养护
var vm = new Vue({
    el: '#app',
    data: function() {
        return {
            p: 0,
            pageSize: 20,
            totalPage: 0,
            menu1: {
                "chosedId": [],
                "current": '',
                "list": [{
                    "name": "已完成",
                    "chosed": false
                }, {
                    "name": "执行中",
                    "chosed": false
                }, {
                    "name": "待执行",
                    "chosed": false
                }]
            },
            menu2: {
                "chosedId": [],
                "current": '',
                "list": [{
                    "id": "1",
                    "name": "土建",
                    "chosed": false
                }, {
                    "name": "机电",
                    "chosed": false
                }, {
                    "name": "保洁",
                    "chosed": false
                }, {
                    "name": "绿化",
                    "chosed": false
                }, {
                    "name": "道路",
                    "chosed": false
                }, {
                    "name": "排水",
                    "chosed": false
                }, {
                    "name": "其他",
                    "chosed": false
                }]
            },
            menu3: {
                "chosedId": [],
                "current": '',
                "list": [{
                    "name": "长期作业",
                    "chosed": false
                }, {
                    "name": "短期作业",
                    "chosed": false
                }, {
                    "name": "临时作业",
                    "chosed": false
                }, {
                    "id": "4",
                    "name": "移动作业",
                    "chosed": false
                }]
            },
            dataList: []
        }
    },
    mounted: function() {
        var that = this;
        var complete_status = GetQueryString('complete_status') || '';
        var projecttype = GetQueryString('projecttype') || '';
        var roadclosure = GetQueryString('roadclosure') || '';
        if(complete_status !== '') {
            that.menu1.current = complete_status;
            that.matchFilter('1', complete_status); //处理选中URL带过来的过滤条件,1代表执行状态
        }
        if(projecttype !== '') {
            that.menu2.current = projecttype;
            that.matchFilter('2', projecttype); //处理选中URL带过来的过滤条件,2代表作业类型
        }
        if(roadclosure !== '') {
            that.menu3.current = roadclosure;
            that.matchFilter('3', roadclosure); //处理选中URL带过来的过滤条件,3代表封道类型
        }
        mui.init();
        mui('.mui-scroll-wrapper').scroll({
            deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
        });
        that.getData();
        mui.ready(function() {

            $('#listBox').on('tap', '.icon-dianhua', function() {
                var tel = $(this).data('tel');
                window.location.href = 'tel:' + tel;
            })
        })
    },
    methods: {
        getData: function(loadtype) {
            loading.show();
            var that = this;
            mui('.mui-scroll-wrapper').scroll().scrollTo(0, 0, 0); //初始回到顶部
            if(loadtype !== 0) that.p++;
            $.getJSON('/define/getPlanToday', {
                "page": that.p, //页码
                "perpage": that.pageSize, //每页条目
                "complete_status": that.menu1.current, //
                "projecttype": that.menu2.current, //
                "roadclosure": that.menu3.current //
            }, function(res) {
                that.dataList = res.data;
                that.totalPage = Math.ceil(res.total / that.pageSize);
                loading.hide();
            })
        },
        pulldownRefresh: function() {
            var that = this;
            that.getData();
        },
        pullupRefresh: function() {
            var that = this;
            that.getData();
        },
        stateChangeMenu: function(index) { //状态筛选
            var that = this;
            var item = that.menu1.list[index];
            item.chosed = !item.chosed;
            that.checkFilter();
        },
        zyChangMenu: function(index) { //作业类型筛选
            var that = this;
            //			that.menu2.chosedId = menu.id;
            //			that.menu2.chosedName = menu.name;
            var item = that.menu2.list[index];
            item.chosed = !item.chosed;
            that.checkFilter();
        },
        fdChangeMenu: function(index) { //封道类型筛选
            var that = this;
            var item = that.menu3.list[index];
            item.chosed = !item.chosed;
            that.checkFilter();
        },
        checkFilter: function() {
            var that = this;
            var complete_status = []; //状态
            var projecttype = []; //作业类型
            var roadclosure = []; //封道类型
            $.each(that.menu1.list, function(i, item) {
                if(item.chosed) {
                    complete_status.push(item.name)
                }
            });
            $.each(that.menu2.list, function(i, item) {
                if(item.chosed) {
                    projecttype.push(item.name)
                }
            })
            $.each(that.menu3.list, function(i, item) {
                if(item.chosed) {
                    roadclosure.push(item.name)
                }
            })
            that.menu1.current = complete_status.join(',');
            that.menu2.current = projecttype.join(',');
            that.menu3.current = roadclosure.join(',');
        },
        //跳转页码
        goPage: function(e) {
            var that = this;
            $(e.target).blur();
            that.getData(0);
        },
        //执行筛选
        doFilter: function() {
            var that = this;
            if(that.menu1.current == '' && that.menu2.current == '' && that.menu3.current == '') {
                mui.toast('未选择任何筛选条件');
                return;
            }
            var filterTpl = '<div>所选筛选条件为：<br/>' + that.menu1.current + '<br/>' + that.menu2.current + '<br/>' + that.menu3.current + '</div>';
            mui.confirm(filterTpl, '提示', ['取消', '确定'], function(e) {
                if(e.index == 1) {
                    that.p=1;
                    that.getData(0);
                }
            })
        },
        //匹配url带过来的过滤条件
        matchFilter: function(data, name) {
            var that = this;
            switch(data) {
                case '1':
                    $.each(that.menu1.list, function(i, item) {
                        if(item.name == name) {
                            item.chosed = true;
                        }
                    })
                    break;
                case '2':
                    $.each(that.menu2.list, function(i, item) {
                        if(item.name == name) {
                            item.chosed = true;
                        }
                    })
                    break;
                case '3':
                    $.each(that.menu3.list, function(i, item) {
                        if(item.name == name) {
                            item.chosed = true;
                        }
                    })
                    break;
            }
        }
    },
    filters: {
        timeFormat: function(val) {
            if(!val) return;
            var time = val.substring(5, 20);
            time = time.replace('/', '-');
            return time;
        }
    }
})